package com.asb.service;

import com.aliyun.dingtalktodo_1_0.models.CreateTodoTaskHeaders;
import com.aliyun.dingtalktodo_1_0.models.CreateTodoTaskRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
import com.taobao.api.ApiException;

import cn.hutool.core.util.RandomUtil;

@Deprecated
public class NotifyService {

	String unionid = "7nRd1Oo036MCw7UuK4k5fAiEiE";
	String userid = "1464115634679297";

	private static String access_token;

	public static com.aliyun.dingtalktodo_1_0.Client createClient() throws Exception {
		Config config = new Config();
		config.protocol = "https";
		config.regionId = "central";
		return new com.aliyun.dingtalktodo_1_0.Client(config);
	}

	public void refreshAccessToken() throws ApiException {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
		OapiGettokenRequest request = new OapiGettokenRequest();
		request.setAppkey("dinga7uxhdjvolfqxjsh");
		request.setAppsecret("h3gl-Dq4oXaiTFlvAkewMZha4bhZTsr01e4fN47NRwQmBw-xAZCmxFF4cY6aZmdz");
		request.setHttpMethod("GET");
		OapiGettokenResponse response = client.execute(request);
		access_token = response.getAccessToken();
		System.out.println(response.getBody());
	}

	public String getUserId(String authCode) throws ApiException {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
		OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
		req.setCode(authCode);
		OapiV2UserGetuserinfoResponse rsp = client.execute(req, access_token);
		System.out.println(rsp.getBody());
		return rsp.getResult().getUserid();
	}

	public String notifyWorkUser(String userid) throws ApiException {
		DingTalkClient client = new DefaultDingTalkClient(
				"https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
		OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
		request.setAgentId(1469608097L);
		request.setUseridList(userid);
		request.setToAllUser(false);

		OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
		msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
		msg.getActionCard().setTitle("任务订单审批");
		msg.getActionCard().setMarkdown("# 支持markdown的文本   \n   ## 请审批 ");
		msg.getActionCard().setSingleTitle("订单审批--" + RandomUtil.randomInt());
		msg.getActionCard().setSingleUrl("eapp://pages/switchPages/approve");
		msg.setMsgtype("action_card");
		request.setMsg(msg);
		OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, access_token);
		System.out.println(rsp.getBody());

		return String.valueOf(rsp.getTaskId());
	}

	public void todoTask() throws Exception {
		com.aliyun.dingtalktodo_1_0.Client client = createClient();
		CreateTodoTaskHeaders createTodoTaskHeaders = new CreateTodoTaskHeaders();
		createTodoTaskHeaders.xAcsDingtalkAccessToken = this.access_token;
		CreateTodoTaskRequest.CreateTodoTaskRequestNotifyConfigs notifyConfigs = new CreateTodoTaskRequest.CreateTodoTaskRequestNotifyConfigs()
				.setDingNotify("1");
		CreateTodoTaskRequest createTodoTaskRequest = new CreateTodoTaskRequest()
				.setSourceId("isv_dingtalkTodo" + RandomUtil.randomInt()).setSubject("测试待办任务")
				.setCreatorId("7nRd1Oo036MCw7UuK4k5fAiEiE")
				.setDescription(
						"应用可以调用该接口发起一个钉钉待办任务，该待办事项会出现在钉钉客户端“待办”页面，需要注意的是，通过开放接口发起的待办，目前仅支持直接跳转ISV应用详情页（ISV在调该接口时需传入自身应用详情页链接）。")
				.setDueTime(System.currentTimeMillis() + 1000 * 60 * 5)
				.setExecutorIds(java.util.Arrays.asList("7nRd1Oo036MCw7UuK4k5fAiEiE"))
				.setParticipantIds(java.util.Arrays.asList("7nRd1Oo036MCw7UuK4k5fAiEiE")).setIsOnlyShowExecutor(true)
				.setPriority(20).setNotifyConfigs(notifyConfigs);
		try {
			client.createTodoTaskWithOptions("7nRd1Oo036MCw7UuK4k5fAiEiE", createTodoTaskRequest, createTodoTaskHeaders,
					new RuntimeOptions());
		} catch (TeaException err) {
			err.printStackTrace();
			if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
				// err 中含有 code 和 message 属性，可帮助开发定位问题
			}

		} catch (Exception _err) {
			_err.printStackTrace();
			TeaException err = new TeaException(_err.getMessage(), _err);
			if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
				// err 中含有 code 和 message 属性，可帮助开发定位问题
			}

		}
	}

	public String getAccess_token() {
		return access_token;
	}

}
